#include "stdafx.h"
#include "RemindSummary.h"
#include "ui_RemindSummary.h"
#include "FrameHelper.h"
#include "RemindItem.h"
#include "BaseInclude.h"
#include "RemindHelper.h"
#include "LoginDlg.h"

#include <QFile>
#include <QDesktopServices>
#include <QListWidgetItem>

RemindSummary::RemindSummary(QWidget *parent)
	: QDialog(parent)
{
	ui = new Ui::RemindSummary();
	ui->setupUi(this);
	ui->title->setTitleText(tr("Remind List"));
	pFrameHelper = new FrameHelper(this);
	pFrameHelper->addEdgeWidget(this);
	pFrameHelper->addDragWidget(this);
	pFrameHelper->setMinimizedButton(ui->title->getMinBtn());
	pFrameHelper->setMaxRestoreButton(ui->title->getMaxBtn());
	connect(ui->title->getCloseBtn(), SIGNAL(clicked()), this, SLOT(hide()));
	connect(ui->list, SIGNAL(hideWidget()), this, SLOT(hideWidget()));
	initData();
	initRemindList();
	
}

RemindSummary::~RemindSummary()
{
	disconnect(ui->title->getCloseBtn(), SIGNAL(clicked()), this, SLOT(hide()));
	disconnect(ui->list, SIGNAL(hideWidget()), this, SLOT(hideWidget()));

	delete ui;
	delete pFrameHelper;
}

bool RemindSummary::initRemindList()
{
	const QUserInfo & user = LoginDlg::getUserInfo();
	QRecord dsRecord;
	QString listSQL = QString("SELECT  WAITDOID, OPERNAME, B.USERNAME SENDER, SENDTIME, OPERCONTENT from JXD7_XT_WAITDO A \
		left join(select USERID, USERNAME from jxd7_xt_user) B on A.Sender = B.Userid WHERE(RECEIVERTYPE = 0 AND RECEIVER = '%1') \
		OR(RECEIVERTYPE = 2 AND EXISTS(SELECT 1 FROM JXD7_XT_USER U, JXD7_XT_ORGANISE O1 WHERE U.DEPTID = O1.ORGANISEID \
		AND U.USERID = '%1' AND O1.ORGANISEID = RECEIVER)) OR(RECEIVERTYPE = 1 AND EXISTS (SELECT 1 FROM JXD7_XT_USERROLE R \
		WHERE R.ROLEID = RECEIVER AND R.USERID = '%1')) order by SENDTIME desc").arg(user.strUserID);
	dsRecord.select(listSQL);
	
	QString items;
	for (int row = 0; row < dsRecord.rowCount(); ++row)
	{
		QString itemRow = strItem;
		setIndex(itemRow, row);
		setSender(itemRow, dsRecord.data(row, "SENDER").toString());
		setTitle(itemRow, dsRecord.data(row, "OPERNAME").toString());
		setContent(itemRow, dsRecord.data(row, "OPERCONTENT").toString());
		setTime(itemRow, dsRecord.data(row, "SENDTIME").toString());
		QString url = gMapConfigs.value("HTTP/URL").toString();
		url.replace("[USERNAME]", user.strUserCode).replace("[WAITDOID]", dsRecord.data(row, "WAITDOID").toString());
		setUrl(itemRow, url);
		items.append(itemRow);
	}
	strHtml.replace("[ITEMCONTENT]", items);
	ui->list->setHtml(strHtml);
	return true;
}

void RemindSummary::initData()
{
	QFile fileList(QString("remind:RemindList.html"));
	fileList.open(QFile::ReadOnly | QFile::Unbuffered);
	strHtml = QString(fileList.readAll());
	fileList.close();

	QFile fileItem(QString("remind:Item.html"));
	fileItem.open(QFile::ReadOnly | QFile::Unbuffered);
	strItem = QString(fileItem.readAll());
	fileItem.close();
}

void RemindSummary::hideWidget()
{
	this->showMinimized();
}
